#include<stdio.h>
#include<stdlib.h>
#include<mysql.h>
#include"SELECTFUNC.h"


void conntsql(char* host, char* user, char* passw, int port)
{
	MYSQL mysql;
	mysql_init(&mysql);
	if (mysql_errno(&mysql) == 0)
	{
		printf_s("初始化成功\n");
		mysql_real_connect(&mysql, host, user, passw, NULL, port, NULL, 0);
		if (mysql_errno(&mysql) == 0)
		{
			printf_s("密码正确，验证成功\n");
			return 0;
			
		}
		else
		{
			printf_s("密码错误，正在尝试重新登录");
			printf_s("错误码:%d\n", mysql_errno(&mysql));
			//return conntsql(host, user, passw, port);
		}
	}
	else
	{
		printf_s("初始化失败");
		exit(1);
	}
}
//void show_db(char* host,char* user,char* passw,int* port)
//{
//	/*CONNPAR connect;
//	memset(&connect, 0, sizeof(connect));
//	connect.host = (char*)malloc(20 * sizeof(char*) + 1);
//	connect.host = "192.168.15.251";
//	connect.port = 3306;
//	connect.u_name = (char*)malloc(20 * sizeof(char*) + 1);
//	connect.u_name = "root";
//	connect.password = (char*)malloc(20 * sizeof(char*) + 1);
//	connect.password = "zzq";
//	connect.dbname = (char*)malloc(20 * sizeof(char*) + 1);
//	connect.dbname = "accesslist";*/
//	
//	char *er_msg = NULL;
//	MYSQL mysql;
//	MYSQL_RES *result;
//	MYSQL_ROW row;
//	int num_fields = 0;
//	int i = 0;
//	mysql_init(&mysql);
//er_msg = mysql_error(&mysql);
////printf_s("%u\n", er_code);
//printf_s("%s\n", er_msg);
//if (mysql_errno(&mysql) == 0)
//{
//	printf_s("MYSQL数据库初始化成功\n");
//}
//else
//{
//	printf_s("MSQL数据库初始化失败\n");
//}
//mysql_real_connect(&mysql, host, user, passw, NULL, port, NULL, 0);
//if ((mysql_errno(&mysql)) == 0)
//{
//	mysql_query(&mysql, "show databases");
//	result = mysql_store_result(&mysql);
//	/*mysql c api中打印结果的方法
//	=====================================*/
//	num_fields = mysql_num_fields(result);
//	while ((row = mysql_fetch_row(result)))
//	{
//		for (i = 0; i < num_fields; i++)
//		{
//			printf("%s", row[i]);
//		}
//		printf("\n");
//	}
//	//=====================================
//	mysql_free_result(result);
//	printf_s("%s\n", mysql_error(&mysql));
//	fflush(stdout);
//	mysql_close(&mysql);
//}
//else
//{
//	printf_s("%s\n", mysql_error(&mysql));
//	exit(1);
//}
//
//mysql_select_db(&mysql,);
//mysql_query(&mysql, "show databases");
//result = mysql_store_result(&mysql);
///*row = mysql_fetch_row(result);*/
////=====================================
////mysql c api中打印结果的方法
//num_fields = mysql_num_fields(result); 
//while ((row = mysql_fetch_row(result)))
//{
//	for (i = 0; i < num_fields; i++)
//	{
//		printf("%s", row[i]);
//	}
//	printf("\n");
//}
////=====================================
//mysql_free_result(result);
////printf_s("%s\n", *row);
//printf_s("%s\n", mysql_error(&mysql));
//fflush(stdout);
//mysql_close(&mysql);
//}

//char* strc(void)
//{
//	char* comm;
//	comm = (char*)calloc(50, sizeof(char));
//	char* comm_s = "desc ";
//	char* comm_e;
//	comm_e = (char*)calloc(50, sizeof(char));
//	printf_s("输入表名称：");
//	gets_s(comm_e, 50);
//	strcat_s(comm, 50, comm_s);
//	//printf_s("%s\n", comm);
//	strcat_s(comm, 50, comm_e);
//	printf_s("输入的SQL命令是：%s\n", comm);
//	return comm;
//}
void select_data(char* host, char* user, char* passw, int* port, char* db,char* comm)
{
	MYSQL mysql;
	MYSQL_RES *result;
	MYSQL_ROW row;
	int num_fields = 0;
	int i = 0;
	mysql_init(&mysql);
	if (mysql_errno(&mysql) == 0)
	{
		printf_s("数据库初始化成功\n");
	}
	else
	{
		printf_s("数据库初始化失败\n");
		system("pause");
		exit(2);
	}
	mysql_real_connect(&mysql, host, user, passw, NULL, port, NULL, 0);
	if (mysql_errno(&mysql) == 0)
	{
		mysql_select_db(&mysql, db);
		mysql_query(&mysql, comm);
		result = mysql_store_result(&mysql);
		num_fields = mysql_num_fields(result);
		while (row = mysql_fetch_row(result))
		{
			for (i = 0; i < num_fields; i++)
			{
				printf_s("%s", row[i]);
			}
			printf_s("\n");
		}
		mysql_free_result(result);
	}
	else
	{
		printf_s("%s\n", mysql_error(&mysql));
		exit(1);
	}
	
	mysql_close(&mysql);

}


//生成字典
char* makedict(void)
{
	char* str_pwd;
	char* pwd;
	int i,j,k;
	char *temp;
	FILE *fp=NULL;
	pwd = (char*)calloc(10, sizeof(char));
	str_pwd = (char*)calloc(50, sizeof(char));
	temp = (char*)calloc(10, sizeof(char));
	printf_s("请输入要生成的字典：");
	gets_s(str_pwd, 50);
	//printf_s("%d", strlen(str_pwd));
	for (i = 0; i < strlen(str_pwd); i++)
	{

		for (j = 0; j < strlen(str_pwd); j++)
		{

			for (k = 0; k < strlen(str_pwd); k++)
			{
				*temp = *(str_pwd + i);
				strcat_s(pwd, 10, temp);
				*temp = *(str_pwd + j);
				strcat_s(pwd, 10, temp);
				*temp = *(str_pwd + k);
				strcat_s(pwd, 10, temp);
				fopen_s(&fp, "e://chardict.txt", "a+");
				fprintf_s(fp, pwd);
				fprintf_s(fp, "\n");
				printf_s("%s\n", pwd);
				*pwd = NULL;
				fclose(fp);
			}
		}
	}
	free(pwd);
	free(str_pwd);
	
	
}

//txt文件读取
void fileread(void)
{
	FILE* fp=NULL;
	int i;
	char* temp;
	temp = (char*)calloc(10, sizeof(char));
	fopen_s(&fp, "F:\\test11.txt", "r");
	for (i = 0; i < 4; i++)
	{
		fgets(temp, 10, fp);
		printf_s("%s", temp);
	}
	fclose(fp);

}

